/*
 * Copyright 2015 Umbrela Smart, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.carman.pos.upgrade;

import com.carman.pos.api.Usb;
import com.hisense.pos.hs_msr.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class UpgradeActivity extends Activity implements
        Handler.Callback, Usb.OnUsbChangeListener, Dfu.DfuListener {

    private Usb usb;
    private Dfu dfu;

    private TextView status;
    private ProgressDialog dlProgressDialog;
    private String strTextView = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrade);

        dfu = new Dfu(Usb.USB_VENDOR_ID, Usb.USB_PRODUCT_ID);
        dfu.setListener(this);

        status = (TextView)findViewById(R.id.status);

        Button program = (Button)findViewById(R.id.btnProgram);
        program.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                crtProgressDialog();
            }
        });

        Button enterDfu = (Button)findViewById(R.id.btnEnterDFU);
        enterDfu.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
//                Outputs.enterDfuMode();
            }
        });

        Button leaveDfu = (Button)findViewById(R.id.btnLeaveDFU);
        leaveDfu.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                //Outputs.leaveDfuMode();
                dfu.leaveDfuMode();
                status.append("Leave DFU Mode Success!");
            }
        });
    }

    private Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            status.append(strTextView);
            strTextView = "";
        }
    };

    @Override
    protected void onStart() {
        super.onStart();

        /* Setup USB */
        usb = new Usb(this);
        usb.setUsbManager((UsbManager) getSystemService(Context.USB_SERVICE));
        usb.setOnUsbChangeListener(this);
        usb.setInterfaceID(0);
        // Handle two types of intents. Device attachment and permission
        registerReceiver(usb.getmUsbReceiver(), new IntentFilter(Usb.ACTION_USB_PERMISSION));
        registerReceiver(usb.getmUsbReceiver(), new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
        registerReceiver(usb.getmUsbReceiver(), new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));


        // Handle case where USB device is connected before app launches;
        // hence ACTION_USB_DEVICE_ATTACHED will not occur so we explicitly call for permission
        usb.requestPermission(this, Usb.USB_VENDOR_ID, Usb.USB_PRODUCT_ID);
    }

    @Override
    protected void onStop() {
        super.onStop();

        /* USB */
        dfu.setUsb(null);
        usb.release();
        try {
            unregisterReceiver(usb.getmUsbReceiver());
        } catch (IllegalArgumentException e) { /* Already unregistered */ }
    }

    @Override
    public void onStatusMsg(String msg) {
        // TODO since we are appending we should make the TextView scrollable like a log
//        status.append(msg);
        strTextView += msg;
    }

    @Override
    public void onProcess(int pct) {
//        Log.i("carman","" + pct);
        dlProgressDialog.setProgress(pct);
    }

    @Override
    public boolean handleMessage(Message message) {
        return false;
    }

    @Override
    public void onUsbConnected() {
        final String deviceInfo = usb.getDeviceInfo(usb.getUsbDevice());
        status.setText(deviceInfo);
        dfu.setUsb(usb);
    }

    public void crtProgressDialog(){
        dlProgressDialog = new ProgressDialog(UpgradeActivity.this);
        dlProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dlProgressDialog.setTitle("STM32 DFU Programmer");
//		dlProgressDialog.setIcon(R.drawable.ic_launcher);
        dlProgressDialog.setMessage("Erase page and Upgrading Phase...");
        dlProgressDialog.setMax(100);
        dlProgressDialog.setProgress(0);
        dlProgressDialog.setSecondaryProgress(0);
        dlProgressDialog.setIndeterminate(false);
        dlProgressDialog.setCancelable(false);
        dlProgressDialog.getWindow().setGravity(Gravity.CENTER);
//		dlProgressDialog.getWindow().setDimAmount(0.0f);
        dlProgressDialog.show();
        new Thread(){
            @Override
            public void run() {
                dfu.program();
                dlProgressDialog.dismiss();
                myHandler.sendEmptyMessage(0);
            }
        }.start();
    }

}
